package com.zdlyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdlyh.constants.SystemConstants;
import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.entity.Comment;
import com.zdlyh.domain.entity.User;
import com.zdlyh.enums.AppHttpCodeEnum;
import com.zdlyh.exception.SystemException;
import com.zdlyh.service.CommentService;
import com.zdlyh.mapper.CommentMapper;
import com.zdlyh.service.UserService;
import com.zdlyh.utils.BeanCopyUtils;
import com.zdlyh.domain.vo.CommentVo;
import com.zdlyh.domain.vo.PageVo;
import com.zdlyh.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService{
    @Autowired
    private UserService userService;

    @Override
    public ResponseResult getCommentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        // 查询文章根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        // 传入文章id并且rootId为-1
        // 查询友链评论时不需要文章id，所以当type为0时才传入
        queryWrapper.eq(SystemConstants.Comment_Type_Arictle.equals(commentType),Comment::getArticleId,articleId)
                .eq(Comment::getRootId,-1)
                .eq(Comment::getType,commentType);
        Page<Comment> page = new Page<>(pageNum,pageSize);
        List<Comment> commentList = page(page, queryWrapper).getRecords();

        List<CommentVo> commentVoList = toCommentVoList(commentList);

        // 根据 根评论id查询所对应的子评论集合，并赋值给对应的属性
        for (CommentVo commentVo : commentVoList) {
            // 查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            // 赋值
            commentVo.setChildren(children);
        }

        PageVo pageVo = new PageVo(commentVoList,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id)
                .orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        return toCommentVoList(list);
    }

    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        // 通过CreateBy查询用户的昵称并赋值
        for (CommentVo commentVo : commentVoList) {
            User user = userService.getById(commentVo.getCreateBy());
            // 这里用户可能被删除了，但是评论还可以在
            if (user!=null){
                commentVo.setUsername(user.getNickName());
                commentVo.setAvatar(user.getAvatar());
            }else {
                commentVo.setUsername("此用户已注销");
            }
            // 通过ToCommentUserId给查询用户昵称并赋值
            // ToCommentUserId不为-1才查询
            if (commentVo.getToCommentUserId() != -1){
                if (userService.getById(commentVo.getToCommentUserId()).getNickName()!=null){
                    String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                    commentVo.setToCommentUserName(toCommentUserName);
                }
            }
        }
        return commentVoList;
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        // 判断是否回复的评论
        if (comment.getToCommentId() != -1){
            if (SecurityUtils.getUserId().equals(comment.getToCommentUserId())){
                throw new SystemException(AppHttpCodeEnum.COMMENT_ERROR);
            }
        }

        baseMapper.insert(comment);
        return ResponseResult.okResult();
    }
}




